package com.macro.mall.commission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.commission.domain.UserAccount;
import com.macro.mall.commission.entity.UserAccountEntity;
import com.macro.mall.commission.mapper.UserAccountMapper;
import com.macro.mall.commission.service.UserAccountService;
import com.macro.mall.commission.service.bo.InsertUserAccountInput;
import com.macro.mall.commission.service.bo.PagedQueryUserAccountInput;
import com.macro.mall.common.api.util.BigDecimalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户佣金账户Service
 *
 * @author xuyanjun
 * @date 2019-12-11
 */
@Slf4j
@Service
public class UserAccountServiceImpl implements UserAccountService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    /**
     * 新增用户佣金账户
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertUserAccountInput input) {
        UserAccountEntity userAccountEntity = new UserAccountEntity();
        userAccountEntity.setUserId(input.getUserId())
                .setBalance(0)
                .setTotal(0);

        LocalDateTime now = LocalDateTime.now();
        userAccountEntity.setCreateTime(now);
        userAccountEntity.setUpdateTime(now);

        userAccountMapper.insert(userAccountEntity);
        return userAccountEntity.getId();
    }

    /**
     * 根据Id获取用户佣金账户
     *
     * @param id id
     * @return 用户佣金账户
     */
    @Override
    public UserAccountEntity getById(long id) {
        return userAccountMapper.selectById(id);
    }

    /**
     * 根据Id获取分佣用户账户
     *
     * @param userId 用户id
     * @return 分佣用户账户
     */
    @Override
    public UserAccountEntity getByUserId(long userId) {
        Assert.isTrue(userId > 0, "用户id不能为空。");

        LambdaQueryWrapper<UserAccountEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserAccountEntity::getUserId, userId);

        List<UserAccountEntity> userAccounts = userAccountMapper.selectList(queryWrapper);
        if (userAccounts.size() > 0) {
            return userAccounts.get(0);
        }
        return null;
    }

    /**
     * 根据批量Id获取用户佣金账户
     *
     * @param batchIds 批量ids
     * @return 用户佣金账户集合
     */
    @Override
    public List<UserAccountEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return userAccountMapper.selectBatchIds(batchIds);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的用户佣金账户
     */
    @Override
    public Page<UserAccountEntity> pagedQuery(PagedQueryUserAccountInput input) {
        LambdaQueryWrapper<UserAccountEntity> queryWrapper = Wrappers.lambdaQuery();

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<UserAccountEntity>) userAccountMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param userAccountEntity 实体
     * @return userAccount
     */
    @Override
    public UserAccount entityToDomain(UserAccountEntity userAccountEntity) {
        if (userAccountEntity == null) {
            return null;
        }

        UserAccount userAccount = new UserAccount();
        userAccount.setId(userAccountEntity.getId())
                .setCreateTime(userAccountEntity.getCreateTime())
                .setUpdateTime(userAccountEntity.getUpdateTime())
                .setUserId(userAccountEntity.getUserId())
                .setBalance(BigDecimalUtils.fromFengToYuan(userAccountEntity.getBalance(), 2))
                .setTotal(BigDecimalUtils.fromFengToYuan(userAccountEntity.getTotal(), 2));

        return userAccount;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param userAccountEntities 实体
     * @return userAccounts
     */
    @Override
    public List<UserAccount> entityToDomain(List<UserAccountEntity> userAccountEntities) {
        List<UserAccount> result = new ArrayList<>();
        if (userAccountEntities == null) {
            return result;
        }

        for (UserAccountEntity userAccountEntity : userAccountEntities) {
            UserAccount userAccount = entityToDomain(userAccountEntity);
            result.add(userAccount);
        }
        return result;
    }

}
